<?php

/*
 * This class is used for common same function.
 */

class Model_Member extends Model_Base {

    protected $_name = 'members';
    protected $_primaryKey = 'member_id';

    
    public function  fetchMemberData(){
        $select = $this->select()->setIntegrityCheck(false)
                ->from(array('usr' => 'members'))
                ->join(array('md' => 'members_details'), "usr.member_id = md.member_id");
        $result = $this->fetchAll($select);
        
        $finalArray[''] = '';
        foreach ($result as $user) {
            $finalArray[$user['member_id']] = $user['mem_name']." (".$user['member_email'].") #".$user['member_phone'];
        }
        return $finalArray;
    }
    
    
    public function  fetchMemberAllData($where = "", $limit = "", $offset = "0", $order = 'member_id', $sort = 'ASC'){
        $select = $this->select()->setIntegrityCheck(false)
                ->from(array('usr' => 'members'))
                ->join(array('md' => 'members_details'), "usr.member_id = md.member_id");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        
       # echo $select; exit;
        
        $result = parent::fetchAll($select);
        
        if (!empty($result)) {
            return $result->toArray();
        } else {
            return array();
        }
    }
    
     public function getMemberCount($where) {
        
        $select = $this->select()
                ->from(array('usr' => 'members'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('md' => 'members_details'), "usr.member_id = md.member_id");
        
        
        
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }


    /**
     * Function to check email exist for forgot password.
     *
     * @param  int|string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkEmailForForgotPassword($email) {

        $select = $this->select()->setIntegrityCheck(false)->from(array('lgn' => 'members'))
                ->where('member_email = ?', $email);
                
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return $result->toArray();
        } else {
            return array();
        }
    }

    /**
     * Function to check user exist with email
     *
     * @param  string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkEmailExist($email) {

        $select = $this->select()->setIntegrityCheck(false)
                ->from(array('usr' => 'members'))
                ->join(array('md' => 'members_details'), "usr.member_id = md.member_id")
                ->where('member_email = ?', $email);
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return $result;
        } else {
            return false;
        }
    }
    
    ##Phone Logic ##Phone Logic
    public function checkPhoneExist($email) {

        $select = $this->select()->setIntegrityCheck(false)
                ->from(array('usr' => 'members'))
                ->join(array('md' => 'members_details'), "usr.member_id = md.member_id")
                ->where('member_phone = ?', $email);
        
        #echo $select; exit;
        
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return $result;
        } else {
            return false;
        }
    }

    
    
    
    /**
     * Function to check user exist with email other than current email used in edit mode
     *
     * @param  string $email
     * @param  string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkEmail($currentEmail = '', $email) {

        $select = $this->select()->setIntegrityCheck(false)->from(array('lgn' => 'members'))
                ->where('member_email = ? ', $email)
                ->orWhere('member_email = ? ', $currentEmail);
        $result = $this->fetchAll($select);
        if (count($result) > 1) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Function to check user exist with usr_id
     *
     * @param  int|string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkUserIdExist($userId) {

        $select = $this->select()->setIntegrityCheck(false)->from(array('usr' => 'members'))
                ->where('member_id = ?', $userId);
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('lgn' => 'members'))
                ->setIntegrityCheck(false)
                ->join(array('md' => 'members_details'), "lgn.member_id = md.member_id")
                ->where('md.member_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
       # echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
     public function fetchprefixData() {
        return array(
            '' => '-- Select Prefix --',
            'mr' => 'MR',
            'mrs' => 'MRS',
            'ms' => 'MS',
            'dr' => 'DR'
        );
    }

    public function fetchCompanyData() {
        return array(
            '' => '-- Select Company Type --',
            'real estate' => 'Real Estate',
            'contractors' => 'Contractors',
            'developers' => 'Developers',
            'associates' => 'Associates'
        );
    }   
    
    public function fetchClientData() {
        return array(
            //'' => '-- Select Client Type --',
            'seller' => 'Seller',
            'buyer' => 'Buyer',
            'landlord' => 'Landlord',
            'tenant' => 'Tenant',
            'developer' => 'Developer',
            'associate' => 'Associate',
            'eebsite user' => 'Website User'
        );
    }   
    
     public function fetchClientStatusData() {
        return array(
            '' => '-- Select Client Status --',
            'hot' => 'Hot',
            'warm' => 'Warm',
            'cold' => 'Cold',
            'pending' => 'Pending',
            'incubated' => 'Incubated ',
            'bought' => 'Bought'
        );
    }   
    

}
